package com.rcd.viatornisl.android.model;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

public class InterestManager extends BaseManager {
	
	private final String INTEREST = "interests/";
	private final String INTEREST_JSONLIST = "jsonlist/";
	private final String INTEREST_JSONVIEW = "jsonview/";
	
	public static class InterestListThread implements Runnable {

		private String _BaseUrl;
		private List<Interest> _Interests;
		
		public InterestListThread(String baseUrl, boolean synchronous) {
			// Setup the attributes
			this._BaseUrl = baseUrl;
			this._Interests = null;
												
			try {
				// Create a therad
				Thread interestListThread = new Thread(this);
				
				// Start a thread
				interestListThread.start();
				
				// If synchronous
				if (synchronous) {
					// Set the join
					interestListThread.join(JOIN_TIMEOUT);	
				}					
			} catch (Exception exception) {
				// Log the error
				Log.e("InterestListThread(String baseUrl, boolean synchronous)", exception.getLocalizedMessage());
							
				// On error
				exception.printStackTrace();
			}
		}
		
		public List<Interest> getInterests() {
			return this._Interests;
		}
		
		@Override
		public void run() {
			try {
				// Create an interest manager
				InterestManager interestManager = new InterestManager(this._BaseUrl);
						
				// Load the interests
				this._Interests = interestManager.getInterests();
			} catch (Exception exception) {
				// Log the error
				Log.e("void run()", exception.getLocalizedMessage());
							
				// On error
				exception.printStackTrace();
			}
		}
		
	}
	
	public InterestManager() {
		// Setup the attributes
		this.setBaseUrl("");
	}
	
	public InterestManager(String baseUrl) {
		// Setup the attributes
		this.setBaseUrl(baseUrl);
	}
	
	public Interest getInterest(int id) {
		// Set the right url
		String url = String.format("%s%s%s%d", this.getBaseUrl(), INTEREST, INTEREST_JSONVIEW, id);
		
		try {
			// Get full content JSON from a url
			JSONObject json = new JSONObject(new Scanner(new URL(url).openStream(), "UTF-8").useDelimiter("\\A").next());
			
			// Create an interest object
			Interest interest = new Interest();
			
			// Set the interest properties
			interest.setId(json.getJSONObject("Interest").getInt("id"));
			interest.setName(json.getJSONObject("Interest").getString("name"));
			interest.setDescription(json.getJSONObject("Interest").getString("description"));
			interest.setCreated(interest.loadDate(json.getJSONObject("Interest").getString("created")));
			interest.setModified(interest.loadDate(json.getJSONObject("Interest").getString("modified")));
			
			// Provide the return
			return interest;
		} catch (Exception exception) {
			// Log the error
			Log.e("Interest getInterest(int id)", exception.getLocalizedMessage());
						
			// On error
			exception.printStackTrace();
		}
		
		// Provide the return
		return null;
	}
	
	public List<Interest> getInterests() {
		// Set the right url
		String url = String.format("%s%s%s", this.getBaseUrl(), INTEREST, INTEREST_JSONLIST);
				
		try {
			// Get full content JSON from a url
			JSONArray json = new JSONArray(new Scanner(new URL(url).openStream(), "UTF-8").useDelimiter("\\A").next());

			// Create an interest list
			List<Interest> interests = new ArrayList<Interest>();
						
			// Fill the interests
			for (int index = 0; index < json.length(); index++) {
				// Create a interest object
				Interest interest = new Interest();
				
				// Set the interest properties
				interest.setId(json.getJSONObject(index).getJSONObject("Interest").getInt("id"));
				interest.setName(json.getJSONObject(index).getJSONObject("Interest").getString("name"));
				interest.setDescription(json.getJSONObject(index).getJSONObject("Interest").getString("description"));
				interest.setCreated(interest.loadDate(json.getJSONObject(index).getJSONObject("Interest").getString("created")));
				interest.setModified(interest.loadDate(json.getJSONObject(index).getJSONObject("Interest").getString("modified")));
				
				// Add the interest
				interests.add(interest);
			}
			
			// Provide the return
			return interests;
		} catch (Exception exception) {
			// Log the error
			Log.e("List<Interest> getInterests()", exception.getLocalizedMessage());
						
			// On error
			exception.printStackTrace();
		}
		
		// Provide the return
		return null;
	}
	
}
